package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.CodeUtil;
import com.wing.common.utils.DateUtils;
import com.wing.productionPlan.dao.DynamicDao;
import com.wing.productionPlan.dao.ProductionPieceRateDetailDao;
import com.wing.productionPlan.dao.ProductionProcessDao;
import com.wing.productionPlan.enumeration.PieceRateDetailStatusEnum;
import com.wing.productionPlan.model.entity.ProductionPieceRateDetail;
import com.wing.productionPlan.model.entity.ProductionTheWorkOrder;
import com.wing.productionPlan.model.entity.ProductionWorkOrder;
import com.wing.productionPlan.model.request.ProductionPieceRateDetailForm;
import com.wing.productionPlan.model.response.ProductionPieceRateDetailVO;
import com.wing.productionPlan.model.response.ResBenchmarkBookEchnologyVo;
import com.wing.web.utils.AddUpdateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 计件工资单明细-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductionPieceRateDetailServiceImpl extends ServiceImpl<ProductionPieceRateDetailDao, ProductionPieceRateDetail> implements ProductionPieceRateDetailService {

    @Autowired
    private DynamicDao dynamicDao;
    @Autowired
    private ProductionProcessDao processDao;
    @Autowired
    private ProductionPieceRateService pieceRateService;
    
    @Override
    public int add(ProductionPieceRateDetailForm productionPieceRateDetailForm) {
        //校验
        ProductionPieceRateDetail productionPieceRateDetail =new ProductionPieceRateDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductionPieceRateDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productionPieceRateDetailForm, productionPieceRateDetail, copyOptions);
        //day、no
        String today = DateUtil.format(new Date(), DateUtils.DATE_PATTERN_FORMAT);
        Integer maxNo = dynamicDao.getDayMaxNo("ord_production_piece_rate_detail");
        String code = CodeUtil.getCode(maxNo);
        productionPieceRateDetail.setNo(Integer.valueOf(code));
        productionPieceRateDetail.setDay(Integer.valueOf(today));
        AddUpdateUtil.fillInsertInfo(productionPieceRateDetail);
        return baseMapper.insert(productionPieceRateDetail);
    }

    @Override
    public List<ProductionPieceRateDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductionPieceRateDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductionPieceRateDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductionPieceRateDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public void create(ProductionTheWorkOrder theWorkOrder, ProductionWorkOrder workOrder) {
        /**1.添加计件工资明细*/
        ProductionPieceRateDetailForm pieceRateDetail = new ProductionPieceRateDetailForm();
        pieceRateDetail.setSkuCode(workOrder.getSkuCode());
        pieceRateDetail.setProcessId(theWorkOrder.getProcessId());
        pieceRateDetail.setProcessName(theWorkOrder.getProcessName());
        pieceRateDetail.setTheWorkOrderPeople(theWorkOrder.getCreator());
        pieceRateDetail.setTheWorkOrderPeopleName(theWorkOrder.getCreatorName());
        //单价
        ResBenchmarkBookEchnologyVo echnology = processDao.getBenEchnologyByProcessId(theWorkOrder.getProcessId());
        if (ObjectUtil.isNull(echnology)) {
            throw new BusinessException("未找到对应工序");
        }
        Integer workQuantity = theWorkOrder.getWorkQuantity();
        Integer qualifiedNoQuantity = theWorkOrder.getQualifiedNoQuantity();
        Integer qualifiedQuantity = workQuantity - qualifiedNoQuantity;
        BigDecimal salaryQuota = echnology.getSalaryQuota();
        pieceRateDetail.setSalaryQuota(salaryQuota);
        pieceRateDetail.setReportedQuantity(workQuantity);
        pieceRateDetail.setOkQuantity(qualifiedQuantity);
        pieceRateDetail.setOkSalary(salaryQuota.multiply(BigDecimal.valueOf(qualifiedQuantity.longValue())));
        pieceRateDetail.setQualifiedNoQuantity(qualifiedNoQuantity);
        pieceRateDetail.setQualifiedNoSalary(salaryQuota.multiply(BigDecimal.valueOf(qualifiedNoQuantity.longValue())));
        pieceRateDetail.setStatus(PieceRateDetailStatusEnum.CONFIRMED.getCode());
        pieceRateDetail.setConfirmTime(LocalDateTime.now());
        pieceRateDetail.setTheWorkOrderId(theWorkOrder.getId());
        pieceRateDetail.setMonth(Integer.valueOf(DateUtil.format(new Date(), DateUtils.DATE_PATTERN_FORMAT_MONTH2)));
        add(pieceRateDetail);
        /**2.创建计件工资单*/
        pieceRateService.create2(theWorkOrder.getCreator());
    }
}
